//
//  MomentCell.m
//  MomentKit
//
//  Created by LEA on 2017/12/14.
//  Copyright © 2017年 LEA. All rights reserved.
//

#import "MomentCell.h"

#pragma mark - ------------------ 动态 ------------------

// 最大高度限制
CGFloat maxLimitHeight = 0;

@implementation MomentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI
{
    
    // 头像视图
    _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 * SCREENH6, kBlank, kFaceWidth, kFaceWidth)];
    _headImageView.contentMode = UIViewContentModeScaleAspectFill;
    _headImageView.userInteractionEnabled = YES;
    _headImageView.layer.cornerRadius = 20 * SCREENH6;
    _headImageView.layer.masksToBounds = YES;
    [self.contentView addSubview:_headImageView];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickHead:)];
    [_headImageView addGestureRecognizer:tapGesture];
    // 名字视图
    _nameLab = [[UILabel alloc] initWithFrame:CGRectMake(_headImageView.right + 10 * SCREENH6, _headImageView.top, kTextWidth, 15 * SCREENH6)];
    _nameLab.font = [UIFont boldSystemFontOfSize:15.0];
    _nameLab.textColor = kHLTextColor;
    _nameLab.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_nameLab];
    // 评分
    _starView = [[XHStarRateView alloc] initWithFrame:CGRectMake(_headImageView.right + 10 * SCREENH6, _nameLab.bottom + 5 * SCREENH6, 70 * SCREENH6, 10 * SCREENH6)];
    _starView.isAnimation = YES; // 有动画
    _starView.rateStyle = XHStarRateViewRateStyeFullStar; //允许不完整星评论
    //    starRateView.delegate = self;
    _starView.currentRating = 5;
    _starView.userInteractionEnabled = NO;
    [self.contentView addSubview:_starView];
    // 时间
    _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_headImageView.right + 190 * SCREENH6, _nameLab.top, kTextWidth, 15 * SCREENH6)];
    _timeLabel.font = [UIFont systemFontOfSize:13.0];
    _timeLabel.textColor = [UIColor lightGrayColor];
    _timeLabel.backgroundColor = [UIColor clearColor];
//    _timeLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_timeLabel];
    // 正文视图
    _linkLabel = kMLLinkLabel();
    _linkLabel.font = kTextFont;
    _linkLabel.delegate = self;
    _linkLabel.linkTextAttributes = @{NSForegroundColorAttributeName:kLinkTextColor};
    _linkLabel.activeLinkTextAttributes = @{NSForegroundColorAttributeName:kLinkTextColor,NSBackgroundColorAttributeName:kHLBgColor};
    [self.contentView addSubview:_linkLabel];

    // 图片区
    _imageListView = [[MMImageListView alloc] initWithFrame:CGRectZero];
    [self.contentView addSubview:_imageListView];
    //WithFrame:CGRectMake(10 * SCREENH6, 1 * SCREENH6, self.contentView.bounds.size.width - 20 * SCREENH6, 1 * SCREENH6)
    _lineView = [[UIView alloc] init];
    _lineView.backgroundColor = ColorWithRGB(0xe4e4e4);
    [self.contentView addSubview:_lineView];

    // 最大高度限制
    maxLimitHeight = _linkLabel.font.lineHeight * 6;
}

- (void)setType:(NSString *)type{
    
    _type = type;
    
    if ([type isEqualToString:@"goods"]) {
        
        _starView.hidden = YES;
        _timeLabel.hidden = YES;
    }else{
        
        _starView.hidden = NO;
        _timeLabel.hidden = NO;
    }
}

#pragma mark - setter
- (void)setMoment:(Moment *)moment
{
    _moment = moment;
    int current = [moment.merchants_goods_comments_star_level intValue];
    _starView.currentRating = current;
//    NSLog(@"inputtime===%@",moment.inputtime);
    _timeLabel.text = moment.inputtime;
    if([moment.head_img containsString:@"http"]) {
        
        NSURL * url = [NSURL URLWithString:moment.head_img];
        // 头像
        [_headImageView sd_setImageWithURL:url];
    } else {
        
        NSString * iconStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,moment.head_img];
        NSURL * url = [NSURL URLWithString:iconStr];
        // 头像
        [_headImageView sd_setImageWithURL:url];
    }
    
    // 昵称
    _nameLab.text = moment.user_name;
    // 正文
//    _showAllBtn.hidden = YES;
    _linkLabel.hidden = YES;
    CGFloat bottom = _headImageView.bottom + kPaddingValue;
    CGFloat rowHeight = 0;
    if ([moment.comment_content length]) {
        _linkLabel.hidden = NO;
        _linkLabel.text = moment.comment_content;
        // 判断显示'全文'/'收起'
        CGSize attrStrSize = [_linkLabel preferredSizeWithMaxWidth:kTextWidth];
        CGFloat labH = attrStrSize.height;

        _linkLabel.frame = CGRectMake(_nameLab.left, bottom, attrStrSize.width, labH);

            bottom = _linkLabel.bottom + kPaddingValue;
    }
    // 图片
    _imageListView.moment = moment;
    NSArray *array = [moment.comment_img componentsSeparatedByString:@","];
//    NSLog(@"count===%lu",(unsigned long)array.count);
    if (array.count > 0) {
        _imageListView.origin = CGPointMake(_nameLab.left, bottom);
        bottom = _imageListView.bottom + kPaddingValue;
    }
    
        rowHeight = _imageListView.bottom + kBlank;
    // 这样做就是起到缓存行高的作用，省去重复计算!!!
    _lineView.frame = CGRectMake(_headImageView.left, rowHeight - 10 * SCREENH6, 355 * SCREENH6, 1 * SCREENH6);
    _moment.rowHeight = rowHeight;
}

#pragma mark - 点击事件

// 点击头像
- (void)clickHead:(UITapGestureRecognizer *)gesture
{
//    if ([self.delegate respondsToSelector:@selector(didClickProfile:)]) {
//        [self.delegate didClickProfile:self];
//    }
}


#pragma mark - MLLinkLabelDelegate
- (void)didClickLink:(MLLink *)link linkText:(NSString *)linkText linkLabel:(MLLinkLabel *)linkLabel
{
    // 点击动态正文或者赞高亮
    if ([self.delegate respondsToSelector:@selector(didClickLink:linkText:)]) {
        [self.delegate didClickLink:link linkText:linkText];
    }
}
@end

#pragma mark - ------------------ 评论 ------------------
@implementation CommentLabel

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _linkLabel = kMLLinkLabel();
        _linkLabel.delegate = self;
        [self addSubview:_linkLabel];
    }
    return self;
}

#pragma mark - Setter
- (void)setComment:(Comment *)comment
{
    _comment = comment;
    _linkLabel.attributedText = kMLLinkLabelAttributedText(comment);
    CGSize attrStrSize = [_linkLabel preferredSizeWithMaxWidth:kTextWidth];
    _linkLabel.frame = CGRectMake(5, 3, attrStrSize.width, attrStrSize.height);
    self.height = attrStrSize.height + 5;
}

#pragma mark - MLLinkLabelDelegate
- (void)didClickLink:(MLLink *)link linkText:(NSString *)linkText linkLabel:(MLLinkLabel *)linkLabel
{
    if (self.didClickLinkText) {
        self.didClickLinkText(link,linkText);
    }
}

#pragma mark - 点击
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.backgroundColor = kHLBgColor;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC));
    dispatch_after(when, dispatch_get_main_queue(), ^{
        self.backgroundColor = [UIColor clearColor];
        if (self.didClickText) {
            self.didClickText(self->_comment);
        }
    });
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.backgroundColor = [UIColor clearColor];
}

@end
